package com.pda.common.tools;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

//字符串处理
public class StringProcess {
    //判断字符串是否 不为空且长度大于0
    public static boolean stringIsZero(String string) {
        if(string==null && string.length()==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //判断字符串长度是否大于size
    public static boolean stringMoreThan(String string, int size) {
        if(string!=null && string.length()>size)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //判断字符串长度是否小于size
    public static boolean stringLessThan(String string, int size) {
        if(string!=null && string.length()<size)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //判断字符串长度是否等于size
    public static boolean stringEquals(String string, int size) {
        if(string!=null && string.length()==size)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //判断手机号
    public static boolean isPhone(String phone) {
        //String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        String regex = "(1)\\d{10}$";
        if (phone == null || phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    //手机号隐藏中间4位
    public static String phoneHide(String phone) {
        if(phone==null || phone.length()<11)
        {
            return "";
        }
        String str = "****";
        StringBuilder sb = new StringBuilder(phone);
        sb.replace(3, 7, str);
        return sb.toString();
    }

    public static String responsetoString(ResponseBody responseBody) {
        BufferedSource source = responseBody.source();
        Buffer buffer = source.buffer();
        Charset charset = Charset.forName("UTF-8");
        return buffer.clone().readString(charset);
    }

    public static SpannableString stringForegroundColorSpan(String string, int pos,String color) {
        SpannableString spannableString = new SpannableString(string);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(color));
        spannableString.setSpan(colorSpan, pos, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static boolean boxcodeJudge(String code) {
        int a = SharedPreferencesManger.getBoxMix();
        int b = SharedPreferencesManger.getBoxMax();
        if (code.length() >= a && code.length() <= b) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean casecodeJudge(String code) {
        int a = SharedPreferencesManger.getCaseMix();
        int b = SharedPreferencesManger.getCaseMax();
        if (code.length() >= a && code.length() <= b) {
            return true;
        } else {
            return false;
        }
    }

    public static String codeCL(String string) {
        String code = string;
        try {
            String[] arr = string.split("&c=");
            code = arr[1];
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return code;
        }
    }
}
